package com.java.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.constant.UserAddressDefault;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.mapper.UserAddressDao;
import com.java.pojo.UserAddress;
import com.java.pojo.bo.AddressBO;
import com.java.service.UserAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;

@Service("userAddressService")
public class UserAddressServiceImpl extends ServiceImpl<UserAddressDao, UserAddress> implements UserAddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserAddress> page = this.page(
                new Query<UserAddress>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<UserAddress>()
        );

        return new PageUtils(page);
    }

    @Override
    public void addNewUserAddress(AddressBO addressBO) {
        // 判断用户使用有收货地址,如果没有,则新增为'默认地址'
        int count = this.count(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, addressBO.getUserId()));
        Integer isDefault = UserAddressDefault.NO_DEFAULT;
        if (count <= 0) {
            isDefault = UserAddressDefault.DEFAULT;
        }
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(addressBO,userAddress);
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        long id = snowflake.nextId();
        userAddress.setId(String.valueOf(id));
        userAddress.setIsDefault(isDefault);
        this.save(userAddress);
    }

    @Override
    public void updateUserAddress(AddressBO addressBO) {
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(addressBO,userAddress);
        userAddress.setId(addressBO.getAddressId());
        this.updateById(userAddress);
    }

    @Transactional
    @Override
    public void updateUserAddressToBeDefault(String userId, String addressId) {
        // 将原默认地址设置非默认
        UserAddress oldDefaultUserAddress = this.baseMapper.selectOne(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId).eq(UserAddress::getIsDefault,UserAddressDefault.DEFAULT));
        oldDefaultUserAddress.setIsDefault(UserAddressDefault.NO_DEFAULT);
        // 设置新的默认地址
        UserAddress newDefaultUserAddress = this.baseMapper.selectOne(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId).eq(UserAddress::getId,addressId));
        newDefaultUserAddress.setIsDefault(UserAddressDefault.DEFAULT);

        this.updateBatchById(Arrays.asList(oldDefaultUserAddress,newDefaultUserAddress));
    }

}